package com.zm.demo.core.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2020/1/12.
 */
public class LevelOrderBottom {

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>>lists = new ArrayList<>();
        if(root == null){
            return lists;
        }

        levelOrderBottom(root, lists, 0);

        List<List<Integer>>result = new ArrayList<>();
        for(int j = lists.size()-1; j>=0; j--){
            result.add(lists.get(j));
        }
        return lists;

    }

    public void levelOrderBottom(TreeNode root, List<List<Integer>>lists, int level) {
        if(root != null){
            if(lists.size() == level || lists.get(level) == null){
                List<Integer> l= new ArrayList<>();
                l.add(root.val);
                lists.add(l);
            }else{
                lists.get(level).add(root.val);
            }
            if(root.left != null){
                levelOrderBottom(root.left, lists, level+1);
            }

            if(root.right != null){
                levelOrderBottom(root.right, lists, level+1);
            }
        }
    }
}
